package com.leyou.page.service.impl;

import com.leyou.common.exception.LyException;
import com.leyou.common.utils.JsonUtils;
import com.leyou.item.client.ItemClient;
import com.leyou.item.dto.*;
import com.leyou.page.dto.SpecGroupVO;
import com.leyou.page.dto.SpecParamVO;
import com.leyou.page.service.GoodsPageService;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: SGS
 * @Date : 2020/11/25 17:57
 * @Version :
 */
@Service
public class GoodsPageServiceImpl implements GoodsPageService {
    private final ItemClient itemClient;
    private final StringRedisTemplate redisTemplate;
    ;

    public GoodsPageServiceImpl(ItemClient itemClient, StringRedisTemplate redisTemplate) {
        this.itemClient = itemClient;
        this.redisTemplate = redisTemplate;
    }

    private static final String KEY_PREFIX_SPU = "page:spu:id:";
    private static final String KEY_PREFIX_SKU = "page:sku:id:";
    private static final String KEY_PREFIX_DETAIL = "page:detail:id:";
    private static final String KEY_PREFIX_CATEGORY = "page:category:id:";
    private static final String KEY_PREFIX_BRAND = "page:brand:id:";
    private static final String KEY_PREFIX_SPEC = "page:spec:id:";

    @Override
    public String loadSpuData(Long spuId) {
        //查询信息
        SpuDTO spuDTO = itemClient.querySpuById(spuId);
        if (spuDTO == null) {
            throw new LyException(404, "商品不存在");
        }
        //组织数据
        HashMap<String, Object> map = new HashMap<>();
        map.put("id", spuDTO.getId());
        map.put("name", spuDTO.getName());
        map.put("categoryIds", spuDTO.getCategoryIds());
        map.put("brandId", spuDTO.getBrandId());
        String json = JsonUtils.toJson(map);
        //存入redis,如果数据量逐渐增多,可以用SSDB代替
        redisTemplate.opsForValue().set(KEY_PREFIX_SPU + spuId, json);

        return json;
    }

    @Override
    public String loadSpuDetailData(Long spuId) {
        SpuDetailDTO detail = itemClient.queryDetailById(spuId);
        if (detail == null) {
            throw new LyException(404, "商品不存在");
        }
        String json = JsonUtils.toJson(detail);
        redisTemplate.opsForValue().set(KEY_PREFIX_DETAIL + spuId, json);
        return json;
    }

    @Override
    public String loadSkuListData(Long spuId) {
        List<SkuDTO> skuList = itemClient.querySkuBySpuId(spuId);
        if (skuList == null || skuList.size() <= 0) {
            throw new LyException(404, "商品不存在");
        }
        String json = JsonUtils.toJson(skuList);
        redisTemplate.opsForValue().set(KEY_PREFIX_SKU + spuId, json);
        return json;
    }

    @Override
    public String loadCategoriesData(List<Long> ids) {
        List<CategoryDTO> list = itemClient.queryCategoryByIds(ids);
        if (list == null || list.size() <= 0) {
            throw new LyException(404, "商品不存在");
        }
        List<HashMap<String, Object>> maps = list.stream().map(categoryDTO -> {
            HashMap<String, Object> map = new HashMap<>();
            map.put("id", categoryDTO.getId());
            map.put("name", categoryDTO.getName());
            return map;
        }).collect(Collectors.toList());
        String json = JsonUtils.toJson(maps);
        redisTemplate.opsForValue().set(KEY_PREFIX_CATEGORY + ids.get(2), json);
        return json;
    }

    @Override
    public String loadBrandData(Long brandId) {
        BrandDTO brand = itemClient.queryBrandById(brandId);
        if (brand == null) {
            throw new LyException(404, "商品不存在");
        }
        HashMap<String, Object> map = new HashMap<>();
        map.put("id", brand.getId());
        map.put("name", brand.getName());
        String json = JsonUtils.toJson(map);
        redisTemplate.opsForValue().set(KEY_PREFIX_BRAND + brandId, json);
        return json;

    }

    @Override
    public String loadSpecData(Long categoryId) {
        List<SpecGroupDTO> list = itemClient.querySpecList(categoryId);
        if (list == null || list.size() <= 0) {
            throw new LyException(404, "商品不存在");
        }

        ArrayList<SpecGroupVO> groupVOList = new ArrayList<>(list.size());

        for (SpecGroupDTO specGroupDTO : list) {
            //创建vo对象
            SpecGroupVO groupVO = new SpecGroupVO();
            groupVOList.add(groupVO);

            groupVO.setName(specGroupDTO.getName());

            List<SpecParamVO> params = specGroupDTO
                    .getParams().stream()
                    .map(specParamDTO ->
                            specParamDTO.toEntity(SpecParamVO.class))
                    .collect(Collectors.toList());
            groupVO.setParams(params);
        }
        String json = JsonUtils.toJson(groupVOList);
        redisTemplate.opsForValue().set(KEY_PREFIX_SPEC + categoryId, json);

        return json;
    }
}
